<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class GroupAction extends AdminAction {
    
    #用户组列表
    public function index(){ 
        $obj = M('role');
        $list = $obj->select();
        $this->assign('list', $list);       
    	$this->display();
    }

    #添加用户组
    public function add(){
    	$this->display();
    }

    #保存用户组
    public function save(){

        $obj = D('Role');

        if ($date = $obj->create()) {
            if ($obj->add($date)) {
                $state = 'SUCCESS';
                $message = '用户组添加成功';
                $url = U('Group/index');
            } else {
                $state = '用户组添加失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    #编辑用户组
    public function edit(){

        $id = (int) I('get.id');
        if (!isset ($id)) {
            $this->error('参数错误');
        }
        $obj = M('Role');
        $list = $obj->where('id=' . $id)->find();
        $this->assign('list', $list);
        $this->display();
    }

    #更新用户组
    public function update(){

        $obj = D('Role');

        if ($date = $obj->create()) {
            if ($obj->save($date)) {
                $state = 'SUCCESS';
                $message = '用户组编辑成功';
                $url = U('Group/index');
            } else {
                $state = '用户组编辑失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    //删除组
    public function del() {
        $id = $_GET['id'];
        if (!isset ($id)) {
            $this->error("您没有选择任何信息！");
        }

        $obj = M('Role');
        $list = $obj->where('id=' . $id)->delete();
        
        $access = M('access');
        $access->where('role_id=' . $id)->delete();

        if(!$list){
            echo '0';
        }else{
            echo '1';
        }
    }

    #用户组权限管理
    public function competence() {
        $id = $_GET['id'];
        if (!isset ($id)) {
            $this->error("参数错误！");
        }
        
        $obj = M('Role');
        $list = $obj->field('name')->where('id='.$id)->find();
        $this->assign('name',$list['name']);
        
        $obj = M('access');
        $node = $obj->field('node_id')->where('role_id='.$id)->select();
        
        for($i=0; $i<count($node); $i++){
            $nodeArr[$i]=$node[$i]['node_id'];      
        }
                
        $obj = M('node');
        $list = $obj->where('level=2')->select();

        foreach ($list as $key => $value) {
            $action = $obj->where('pid='.$value['id'])->select();
            $list[$key]['child'] = $action;
        }
        $this->assign('nodeArr',$nodeArr);
        $this->assign('id',$id);
        $this->assign('list',$list);
        $this->display();
    }

    #保存用户组权限
    public function access(){       
        $mod = I('post.mod');
        $role_id = I('post.role_id');
        
        if(!isset($role_id)||$role_id==''){
            $this->error("参数错误！");
        }
        
        if(!isset($mod)||$mod==''){
            $this->error("您没有选择任何信息！");
        }
        
        $obj = M('access');
        $obj->where('role_id='.$role_id)->delete();
        
        $data['role_id'] = $role_id;        
        $data['node_id'] = 1;   
        $data['level'] = 1;
        $data['pid'] = 0;
        $obj->add($data);
        
        foreach ($mod as $v){
            $arr = explode("-",$v);         
            $data['role_id'] = $role_id;
            $data['node_id'] = $arr[0];
            $data['level'] = $arr[1];
            $data['pid'] = $arr[2];
            $obj->add($data);
        }
        
        $this->success('权限设置成功');
        
    }
}